<?php
require 'common.inc.php';
// require '../common.inc.php';
//$DT_MOB['browser'] == 'weixin' or exit('Not IN WeiXin');
$openid = get_cookie('weixin_openid');

$openid = decrypt($openid, DT_KEY.'WXID');
$head_title = "招标搜索".$DT['seo_delimiter'].$head_title;
$foot = 'zhaobiao';
//获取openid目前是固定死的，richie这个用户
// $openid="oIvzcwBzC4Rd2gAV7Z6BF65V5vfY";

//搜索

if($action=='search'){
	$condition=" status=3 ";
	if($searchname){
		//搜索内容
//		$result = $db->query("SELECT * FROM {$DT_PRE}article_data_23 WHERE content like '%{$searchname}%'");
		$tiaojian='';
//		while($r = $db->fetch_array($result)) {
//		if($r['itemid']){
//			$tiaojian.=$r['itemid'].",";
//			}	
//		}
		if($tiaojian){
		$tj=rtrim($tiaojian, ','); 	
		$condition.=" and (title like '%{$searchname}%' or itemid in ({$tj}))";
		}else{
			$condition.=" and title like '%{$searchname}%'";
		}
		
		$now=time();
	    $db->query("insert into {$DT_PRE}weixin_searchlog(`openid`,`keyword`,`addtime`) value('".$openid."','".$searchname."','".$now."')");
	}
	//行业
	if($subtype){
		$hy=explode(",",$subtype);
		foreach($hy as $a=>$b){
		$condition1.=" filtercategory like '%{$b}%' or ";	
			}
	$condition1=substr($condition1,0,strlen($condition1)-3); 
	$condition=$condition." and (".$condition1.")";
	}	
	
	//地区
	if($scope){
		$dq=explode(",",$scope);
		foreach($dq as $c=>$d){
		$condition2.=" filteraddress like '%{$d}%' or ";	
			}
	$condition2=substr($condition2,0,strlen($condition2)-3); 
	$condition=$condition." and (".$condition2.")";
	}
	
	//招标分类
	if($subfl){
		if($subfl!='全部'){
			$fl=explode(",",$subfl);
			foreach($fl as $e=>$f){
			$r = $db->get_one("SELECT * FROM {$DT_PRE}category WHERE catname='{$f}' and moduleid=23");
			$condition3.=" catid={$r['catid']} or ";
			}
			$condition3=substr($condition3,0,strlen($condition3)-3); 
			$condition=$condition." and (".$condition3.")";	
	     }
	}
	//发布时间
	if($publishtime){
		//开始时间
		if(strpos($publishtime,"_")>2){
			$kstime=substr($publishtime,0,10);
			if($kstime)$condition.=" and addtime>={$kstime}";
			}
		
		//结束时间
		$shijian=strstr($publishtime,"_");
		$shijian=substr($shijian,1);
		if($shijian)$condition.=" and addtime<={$shijian}";
		
	}	
	$r = $db->get_one("SELECT COUNT(*) AS num FROM {$DT_PRE}article_23 WHERE $condition");
		$items = $r['num'];
		$pagesize=500;
		$pages = mobile_pages($items, $page, $pagesize);
		$lists = array();
		if($items) {
		$result = $db->query("SELECT * FROM {$DT_PRE}article_23 WHERE $condition ORDER BY addtime desc LIMIT $offset,$pagesize");
			while($r = $db->fetch_array($result)) {
				$r1= $db->get_one("SELECT * FROM {$DT_PRE}category WHERE catid=".$r['catid']);
				
				$r['adddate'] = timetodate($r['addtime'], 'Y-m-d');
				if($r['areaid']) $r['address'] = area_pos($r['areaid'], '');
				$r['catname']=$r1['catname'];
				// 标题截取2行，拼接...
				$r['title'] = dsubstr($r['title'], 78, '...');
				$r['title']=str_replace($searchname,"<font color='red'>".$searchname."</font>",$r['title']);
				$lists[] = $r;
			}
		}
	
	}
	

	
//取出行业分类
$catef = $db->query("SELECT fid as id, f_cat_name as cat_name, 1 as ischeck, 0 as ishide, f_cat_lg FROM {$DT_PRE}filtercategory WHERE f_cat_sm=0 order by displayorder asc");
        $fenleis = array();
        while($cf = $db->fetch_array($catef)){
            $hangye[] = $cf;
        }
//取出招标分类
		$result = $db->query("SELECT * FROM {$DT_PRE}category WHERE moduleid=23 ORDER BY catid");
			while($r = $db->fetch_array($result)) {
			$fenlei[]=$r;
}
//根据openid读取出每个人最新的10条搜索记录

		$result = $db->query("SELECT * FROM {$DT_PRE}weixin_searchlog WHERE openid='{$openid}' group by keyword ORDER BY addtime desc limit 5");
			while($r = $db->fetch_array($result)) {
			$ssjl[]=$r;
}

//根据openid获取当前的行业订阅信息
$user = $db->get_one("SELECT * FROM {$DT_PRE}weixin_user WHERE openid='{$openid}' ");
$user_cat = $user['filtercategory'];

$rs_cat = $db->query("SELECT * FROM {$DT_PRE}filtercategory WHERE fid in ({$user_cat}) ");
while($r = $db->fetch_array($rs_cat)) {
			$hangye[]=$r;
}
if(!$searchname&&!$subtype&&!$scope&&$subfl=='全部'&&!$publishtime){$action="";}		
include template('zhaobiao', 'mobile');
if(DT_CHARSET != 'UTF-8') toutf8();
?>
